package com.a2020111222.test;

import com.a2020111222.test.controller.testController;
import com.a2020111222.test.pojo.user;
import com.a2020111222.test.pojo.userNotes;
import com.a2020111222.test.service.userService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class testSpring {
//    ApplicationContext接口给单例的bean实行预初始化
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("mappers/applicationContext.xml");
    /**
     * @description:  bean的实例化
     * @param: []
     * @return: void
     * @author Yang
     * @date: 2022/11/5 22:44
     */
    @Test
    public void login() {
//      bean的实例化
        user user = (user)applicationContext.getBean("user_1");
        userService userService =(userService) applicationContext.getBean("userService");
        boolean flag= userService.login("YangSir","123");
        System.out.println(flag);
        System.out.println(user);
    }
    /**
     * @description:  bean的实例化
     * @param: []
     * @return: void
     * @author Yang
     * @date: 2022/11/5 22:54
     */
    @Test
    public void staticUser(){
//        静态
        user user = (user)applicationContext.getBean("user_2");
        System.out.println(user);
//        实例化
        user = (user)applicationContext.getBean("user_3");
        System.out.println(user);
    }
    /**
     * @description:  bean的注解装配
     * @param: []
     * @return: void
     * @author Yang
     * @date: 2022/11/6 15:31
     */
    @Test
    public void annotationTest(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("mappers/applicationContext-Notes.xml");
        testController testController= (testController)applicationContext.getBean("testController");
        testController.save();
    }

    /**
     * @description:  bean的生命周期
     * @param:
     * @return:
     * @author Yang
     * @date: 2022/11/6 15:32
     */
    @Test
    public void beanLife(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("mappers/applicationContext-Notes.xml");
        userNotes userNotes = (userNotes)applicationContext.getBean("userNotes");
        System.out.println(userNotes);
        AbstractApplicationContext ac = (AbstractApplicationContext)applicationContext;
        ac.registerShutdownHook();
    }
}
